//
//  DriverListVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/19.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "DriverListVC.h"
#import "DriverListModel.h"
#import "DriverListCell.h"

@interface DriverListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) DriverListCell *driverListCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (weak, nonatomic) IBOutlet UITextField *fieldSearch;
@property (weak, nonatomic) IBOutlet UIButton *btnSure;
@property (weak, nonatomic) IBOutlet UIView *viewTop;
@property (weak, nonatomic) IBOutlet UIImageView *imgVSearch;
@end

@implementation DriverListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"司机列表"];
    [self setBackLeftButton:@""];
    self.btnSure.backgroundColor = [UIColor cjMainColor];
    
    self.driverListCell = [[DriverListCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
    self.imgVSearch.userInteractionEnabled = YES;
    [self.imgVSearch setTapActionWithBlock:^{
//        [ws.modelArray removeAllObjects];
//        [ws.tableView.mj_header beginRefreshing];
        [ws downRefresh];
    }];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

- (IBAction)btnSureAction:(id)sender {
    
    NSMutableArray *arraySelelct = [[NSMutableArray alloc]init];
    [self.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DriverListModel *model = obj;
        if (model.selected) {
            [arraySelelct addObject:model];
        }
    }];
    
    if (arraySelelct.count > 0) {
        if (self.blockSeletArr) {
            self.blockSeletArr(arraySelelct);
        }
        [self pop];
    }else{
        [CJAlert showStringDismissWithDelay:@"请选择司机"];
    }
}

/// <#Description#>
- (void)loadData {

    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/driver/driverListForShipTeam",CJBaseUrl);
    NSDictionary *dicPar = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"10",
//                             @"authStatus":@"3",
//                             @"isFreeze":@"0",
//                             @"driverType":@"3",// 独立司机
                             @"searchInfo":[CJControl getThreeUnaryValueStr:self.fieldSearch.text],
                             @"branchCodeExt":[CJControl getBranchCode]
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
                
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
//        if (list.count > 0) {
            //            #import "CommonRouteModel.h"
            if ([ws.refreshType isEqualToString:@"down"]) {
                [ws.modelArray removeAllObjects];
            }
            
            [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               NSError *error;
                DriverListModel *model = [[DriverListModel alloc]initWithDictionary:obj error:&error];
                model.selected = NO;
                [ws.modelArray addObject:model];
            }];
            
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshing];
            
            if (ws.modelArray.count == total.intValue) {
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }
           
//        }else{
//            
//            [ws.tableView.mj_header endRefreshing];
//            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        
       [ws.tableView reloadData];
        
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 115;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"DriverListCell";
    DriverListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[DriverListCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    DriverListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfiWithModel:model];
    WS(ws);
    cell.blockSelect = ^(id anyObject) {
         if (model.selected) {
             model.selected = NO;
         }else{
             model.selected = YES;
         }
         [ws.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
    };
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DriverListModel *model = self.modelArray[indexPath.row];
    if (model.selected) {
        model.selected = NO;
    }else{
        model.selected = YES;
    }
    [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + self.viewTop.height + CJTableToTopHeight + 5, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight  - self.viewTop.height - self.btnSure.height - CJTableToTopHeight - 5),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}

@end
